package com.jadcon.bedbreakfast.domain;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("reservationService")
public class ReservationServiceImpl implements ReservationService {

	@Autowired()
	private ReservationRepository reservationRepository;
	@Autowired()
	private SuiteService suiteService;

	public List<Suite> equalsDateRange(String checkIn, String checkOut)
			throws ParseException, SQLException {

		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
		final Date stringCheckIn = sdf.parse(checkIn); // user provided String
		final Date stringCheckOut = sdf.parse(checkOut); // user provided String
		List<Suite> suiteList = suiteService.getSuiteList();
		List<Suite> suitesNotAvailable = new ArrayList<Suite>();
		List<Reservation> reservationList = reservationRepository
				.getReservationList();

		System.out.println("");
		System.out.println("A new search begins - suite list: " + suiteList.size());
		System.out.println("");

		
		for (Reservation reservation : reservationList) { // spin through the
															// reservations
			if (reservation.getCheckIn().equals(stringCheckIn)
					|| reservation.getCheckIn().equals(stringCheckOut)
					|| (reservation.getCheckIn().after(stringCheckIn) && reservation
							.getCheckIn().before(stringCheckOut))
					|| reservation.getCheckOut().equals(stringCheckIn)
					|| reservation.getCheckOut().equals(stringCheckOut)
					|| (reservation.getCheckOut().after(stringCheckIn) && reservation
							.getCheckOut().before(stringCheckOut))) {

				// if there is a match, get the suite number off the
				// reservation, and make a list of unavailable suites
				int number = reservation.getRoomNumber();
				for (Suite suite : suiteList) {
					if (suite.getNumber() == number) {

						if (suitesNotAvailable.contains(suite)) {
							continue;
						} else
						suitesNotAvailable.add(suite);
						System.out.println("Suite " + suite.getNumber() + " is not available");
					}
				}
				// now we should have a list of unavailable suites
			}
		}

		suiteList.removeAll(suitesNotAvailable);
		
		for (Suite suite : suiteList) {
			System.out.println("Suite " + suite.getNumber() + " is available");
		}
		return suiteList;

	}

	public List<Reservation> getReservationList() throws SQLException,
			ParseException {
		return reservationRepository.getReservationList();
	}

	public Reservation findByNumber(int reservationNumber) throws SQLException,
			ParseException {
		return reservationRepository.findByNumber(reservationNumber);
	}

	public void save(Reservation reservation) throws SQLException {
		reservationRepository.save(reservation);

	}

	public List<Reservation> findReservationByRoom(int roomNumber)
			throws SQLException, ParseException {
		return reservationRepository.findReservationByRoom(roomNumber);
	}

	public List<Reservation> findReservationByStartDate(String checkIn)
			throws ParseException, SQLException {
		return reservationRepository.findReservationByStartDate(checkIn);
	}
}
